package j.framework.tags.easyui;

import j.framework.core.DictItem;
import j.framework.core.FApplication;
import j.framework.tags.form.FormTagWriter;
import j.util.StringUtils;
import j.wcs.core.WcsContext;

import javax.servlet.jsp.JspException;
import java.util.List;
import java.util.Map;

/**
 * combobox控件，可从远程加载数据，也可从字典表中加载数据
 * 若从字典表中加载数据，请指定：dictionaryId，若从远程加载，请指定：url参数或doName actionName方法
 * 默认数据格式：[{id:"",text:""}]
 * Created by junhai on 2015/1/17.
 */
public class ComboBoxTag extends BaseEasyuiLoadRemoteDataFormControl {
    private String dictionaryId;//从字典表中加载数据
    private String firstItemText;//一般情况下，与字典表结合使用，显示在第一条，但值为空
    private Boolean editable;
    private String selectedValue;
    private String valueField;
    private String textField;
    private String groupField;//指示要被分组的字段
    //取值范围：remote|local,默认为local
    private String mode;//定义在文本改变时如何加载列表数据。如果组合框（combobox）从服务器加载就设置为 'remote'。当设置为 'remote' 模式时，用户输入的值将会被作为名为 'q' 的 http 请求参数发送到服务器，以获取新的数据。

    private String onLoadSuccess;
    private String onSelect;
    private String onUnselect;

    private List<DictItem> items;


    @Override
    public void writeFormTagBegin(FormTagWriter writer) throws JspException {
        writer.beginTag("select");
    }

    @Override
    public void writeFormTagProperties(FormTagWriter writer) throws JspException {

    }

    @Override
    public void writeEasyuiDataOptions(Map<String, Object> dataOptions) throws JspException {
        super.writeEasyuiDataOptions(dataOptions);
        if(editable!=null){
            dataOptions.put("editable",editable);
        }else{
            dataOptions.put("editable",false);//默认不能修改
        }
        if(StringUtils.isNotEmpty(selectedValue)){
            dataOptions.put("value",selectedValue);
        }else{
            Object _value = getFormDataValue();
            if(null!=_value){
                dataOptions.put("value",_value.toString());
            }else{
                dataOptions.put("value","");
            }
        }
        if(StringUtils.isNotEmpty(valueField)){
            dataOptions.put("valueField",valueField);
        }
        if(StringUtils.isNotEmpty(textField)){
            dataOptions.put("textField",textField);
        }
        if(StringUtils.isNotEmpty(groupField)){
            dataOptions.put("groupField",groupField);
        }
        if(StringUtils.isNotEmpty(mode)){
            dataOptions.put("mode",mode);
        }
        if(StringUtils.isNotEmpty(onLoadSuccess)){
            dataOptions.put("onLoadSuccess",onLoadSuccess);
        }
        if(StringUtils.isNotEmpty(onSelect)){
            dataOptions.put("onSelect",onSelect);
        }
        if(StringUtils.isNotEmpty(onUnselect)){
            dataOptions.put("onUnselect",onUnselect);
        }
    }

    @Override
    public void endBeginTag(FormTagWriter writer) throws JspException {
        writer.writeSomething(">");
        List<DictItem> items = this.items;
        if(items == null && StringUtils.isNotEmpty(dictionaryId)){
            items = FApplication.getInstance().getDictItems(dictionaryId);
        }
        if(items != null) {
            if (StringUtils.isNotEmpty(firstItemText)) {
                writer.writeSomething("<option value=\"\">");
                writer.writeSomething(firstItemText);
                writer.writeSomething("</option>");
            }
            //从字典表中加载数据
            if (items.size() > 0) {
                String _code;
                for(DictItem item : items){
                    if (item.isDisabled()) {
                        continue;
                    }
                    _code = item.getCode();
                    if (StringUtils.isNotEmpty(item.getValue())) {
                        _code = item.getValue();
                    }else if(StringUtils.isNotEmpty(_code)){
                        if(_code.contains("_")){
                            _code = _code.substring(_code.indexOf("_")+1,_code.length());
                        }
                    }
                    writer.writeSomething("<option value=\"");
                    writer.writeSomething(_code);
                    writer.writeSomething("\"");
                    if (null != selectedValue && selectedValue.equals(_code)) {
                        writer.writeSomething(" selected=\"selected\"");
                    }
                    writer.writeSomething(">");
                    writer.writeSomething(item.getName());
                    writer.writeSomething("</option>");
                }
            }
        }
        writer.closeTag("select");
    }

    @Override
    public String getEasyuiClassName() {
        return "easyui-combobox";
    }

    @Override
    protected String getControlWidth() {
        return null;
    }

    public void setDictionaryId(String dictionaryId) {
        this.dictionaryId = dictionaryId;
    }

    public void setFirstItemText(String firstItemText) {
        this.firstItemText = firstItemText;
    }

    public void setEditable(Boolean editable) {
        this.editable = editable;
    }

    public void setSelectedValue(String selectedValue) {
        this.selectedValue = selectedValue;
    }

    public void setValueField(String valueField) {
        this.valueField = valueField;
    }

    public void setTextField(String textField) {
        this.textField = textField;
    }

    public void setGroupField(String groupField) {
        this.groupField = groupField;
    }

    public void setMode(String mode) {
        this.mode = mode;
    }

    public void setOnLoadSuccess(String onLoadSuccess) {
        this.onLoadSuccess = onLoadSuccess;
    }

    public void setOnSelect(String onSelect) {
        this.onSelect = onSelect;
    }

    public void setOnUnselect(String onUnselect) {
        this.onUnselect = onUnselect;
    }

    public void setItems(List<DictItem> items) {
        this.items = items;
    }
}
